home *** CD-ROM | disk | FTP | other *** search
/ Sprite 1984 - 1993 / Sprite 1984 - 1993.iso / src / cmds / gdb-4.5 / dist / bfd / cache.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-06-30  |  6.9 KB  |  313 lines

  1. /* BFD library -- caching of file descriptors.
  2.    Copyright (C) 1990-1991 Free Software Foundation, Inc.
  3.    Hacked by Steve Chamberlain of Cygnus Support (steve@cygnus.com).
  4.  
  5. This file is part of BFD, the Binary File Descriptor library.
  6.  
  7. This program is free software; you can redistribute it and/or modify
  8. it under the terms of the GNU General Public License as published by
  9. the Free Software Foundation; either version 2 of the License, or
  10. (at your option) any later version.
  11.  
  12. This program is distributed in the hope that it will be useful,
  13. but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  15. GNU General Public License for more details.
  16.  
  17. You should have received a copy of the GNU General Public License
  18. along with this program; if not, write to the Free Software
  19. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.  */
  20.  
  21. /*
  22. SECTION
  23.     File Caching
  24.  
  25.     The file caching mechanism is embedded within BFD and allows
  26.     the application to open as many BFDs as it wants without
  27.     regard to the underlying operating system's file descriptor
  28.     limit (often as low as 20 open files).  The module in
  29.     <<cache.c>> maintains a least recently used list of
  30.     <<BFD_CACHE_MAX_OPEN>> files, and exports the name
  31.     <<bfd_cache_lookup>> which runs around and makes sure that
  32.     the required BFD is open. If not, then it chooses a file to
  33.     close, closes it and opens the one wanted, returning its file
  34.     handle. 
  35.  
  36. */
  37.  
  38. /* $Id: cache.c,v 1.15 1991/12/12 01:23:17 sac Exp $ */
  39.  
  40. #include "bfd.h"
  41. #include "sysdep.h"
  42. #include "libbfd.h"
  43.  
  44. /*
  45. INTERNAL_FUNCTION
  46.     BFD_CACHE_MAX_OPEN macro
  47.  
  48. DESCRIPTION
  49.     The maxiumum number of files which the cache will keep open at
  50.     one time.
  51.  
  52. .#define BFD_CACHE_MAX_OPEN 10
  53.  
  54. */
  55.  
  56.  
  57. static int open_files;
  58.  
  59. static bfd *cache_sentinel;    /* Chain of BFDs with active fds we've
  60.                    opened */
  61.  
  62. /*
  63. INTERNAL_FUNCTION
  64.     bfd_last_cache
  65.  
  66. SYNOPSIS
  67.     extern bfd *bfd_last_cache;
  68.  
  69. DESCRIPTION
  70.     Zero, or a pointer to the topmost BFD on the chain.  This is
  71.     used by the <<bfd_cache_lookup>> macro in @file{libbfd.h} to
  72.     determine when it can avoid a function call.
  73. */
  74.  
  75. bfd *bfd_last_cache;
  76.  
  77. /*
  78.  * INTERNAL_FUNCTION
  79.  *     bfd_cache_lookup
  80.  *
  81.  * DESCRIPTION
  82.  *    Checks to see if the required BFD is the same as the last one
  83.  *    looked up. If so then it can use the iostream in the BFD with
  84.  *    impunity, since it can't have changed since the last lookup,
  85.  *    otherwise it has to perform the complicated lookup function 
  86.  *
  87.  * .#define bfd_cache_lookup(x) \
  88.  * .    ((x)==bfd_last_cache? \
  89.  * .      (FILE*)(bfd_last_cache->iostream): \
  90.  * .       bfd_cache_lookup_worker(x))
  91.  *
  92.  *
  93.  */
  94.  
  95. static void bfd_cache_delete();
  96.  
  97.  
  98. static void
  99. DEFUN_VOID(close_one)
  100. {
  101.     bfd *kill = cache_sentinel;
  102.     if (kill == 0)        /* Nothing in the cache */
  103.     return ;
  104.  
  105.     /* We can only close files that want to play this game.  */
  106.     while (!kill->cacheable) {
  107.     kill = kill->lru_prev;
  108.     if (kill == cache_sentinel) /* Nobody wants to play */
  109.        return ;
  110.     }
  111.  
  112.     kill->where = ftell((FILE *)(kill->iostream));
  113.     bfd_cache_delete(kill);
  114. }
  115.  
  116. /* Cuts the BFD abfd out of the chain in the cache */
  117. static void 
  118. DEFUN(snip,(abfd),
  119.       bfd *abfd)
  120. {
  121.   abfd->lru_prev->lru_next = abfd->lru_next;
  122.   abfd->lru_next->lru_prev = abfd->lru_prev; 
  123.   if (cache_sentinel == abfd) cache_sentinel = (bfd *)NULL;
  124. }
  125.  
  126. static void
  127. DEFUN(bfd_cache_delete,(abfd),
  128.       bfd *abfd)
  129. {
  130.   fclose ((FILE *)(abfd->iostream));
  131.   snip (abfd);
  132.   abfd->iostream = NULL;
  133.   open_files--;
  134. #if 0
  135.   free(fileBuffers[open_files]);
  136. #endif
  137.   bfd_last_cache = 0;
  138. }
  139.   
  140. static bfd *
  141. DEFUN(insert,(x,y),
  142.       bfd *x AND
  143.       bfd *y)
  144. {
  145.   if (y) {
  146.     x->lru_next = y;
  147.     x->lru_prev = y->lru_prev;
  148.     y->lru_prev->lru_next = x;
  149.     y->lru_prev = x;
  150.  
  151.   }
  152.   else {
  153.     x->lru_prev = x;
  154.     x->lru_next = x;
  155.   }
  156.   return x;
  157. }
  158.  
  159.  
  160. /*
  161. INTERNAL_FUNCTION
  162.     bfd_cache_init
  163.  
  164. SYNOPSIS
  165.     void  bfd_cache_init (bfd *);
  166.  
  167. DESCRIPTION
  168.     Initialize a BFD by putting it on the cache LRU.
  169. */
  170.  
  171. void
  172. DEFUN(bfd_cache_init,(abfd),
  173.       bfd *abfd)
  174. {
  175.   cache_sentinel = insert(abfd, cache_sentinel);
  176. }
  177.  
  178.  
  179. /*
  180. INTERNAL_FUNCTION
  181.     bfd_cache_close
  182.  
  183. DESCRIPTION
  184.     Remove the BFD from the cache. If the attached file is open,
  185.     then close it too.
  186.  
  187. SYNOPSIS
  188.     void bfd_cache_close (bfd *);
  189. */
  190. void
  191. DEFUN(bfd_cache_close,(abfd),
  192.       bfd *abfd)
  193. {
  194.   /* If this file is open then remove from the chain */
  195.   if (abfd->iostream) 
  196.     {
  197.       bfd_cache_delete(abfd);
  198.     }
  199. }
  200.  
  201. #ifndef MYBUFSIZE
  202. #if 0
  203. /* For buffering of the files, to speed up fread. */
  204. #define MYBUFSIZE 65536
  205. (char *fileBuffers)[BFD_CACHE_MAX_OPEN];
  206. #endif
  207. #endif
  208.  
  209. /*
  210. INTERNAL_FUNCTION
  211.     bfd_open_file
  212.  
  213. DESCRIPTION
  214.     Call the OS to open a file for this BFD.  Returns the FILE *
  215.     (possibly null) that results from this operation.  Sets up the
  216.     BFD so that future accesses know the file is open. If the FILE
  217.     * returned is null, then there is won't have been put in the
  218.     cache, so it won't have to be removed from it.
  219.  
  220. SYNOPSIS
  221.     FILE* bfd_open_file(bfd *);
  222. */
  223.  
  224. FILE *
  225. DEFUN(bfd_open_file, (abfd),
  226.       bfd *abfd)
  227. {
  228.   abfd->cacheable = true;    /* Allow it to be closed later. */
  229.   if(open_files >= BFD_CACHE_MAX_OPEN) {
  230.     close_one();
  231.   }
  232.   switch (abfd->direction) {
  233.   case read_direction:
  234.   case no_direction:
  235.     abfd->iostream = (char *) fopen(abfd->filename, FOPEN_RB);
  236.     break;
  237.   case both_direction:
  238.   case write_direction:
  239.     if (abfd->opened_once == true) {
  240.       abfd->iostream = (char *) fopen(abfd->filename, FOPEN_RUB);
  241.       if (!abfd->iostream) {
  242.     abfd->iostream = (char *) fopen(abfd->filename, FOPEN_WUB);
  243.       }
  244.     } else {
  245.       /*open for creat */
  246.       abfd->iostream = (char *) fopen(abfd->filename, FOPEN_WB);
  247.       abfd->opened_once = true;
  248.     }
  249.     break;
  250.   }
  251.   if (abfd->iostream) {
  252. #if 0
  253.     fileBuffers[open_files] = (char *)malloc(MYBUFSIZE);
  254.     setbuffer(abfd->iostream, fileBuffers[open_files], MYBUFSIZE);
  255. #endif
  256.     open_files++;
  257.     bfd_cache_init (abfd);
  258.   }
  259.  
  260.   return (FILE *)(abfd->iostream);
  261. }
  262.  
  263. /*
  264. INTERNAL_FUNCTION
  265.     bfd_cache_lookup_worker
  266.  
  267. DESCRIPTION
  268.     Called when the macro <<bfd_cache_lookup>> fails to find a
  269.     quick answer. Finds a file descriptor for this BFD.  If
  270.     necessary, it open it. If there are already more than
  271.     BFD_CACHE_MAX_OPEN files open, it trys to close one first, to
  272.     avoid running out of file descriptors.  
  273.  
  274. SYNOPSIS
  275.     FILE *bfd_cache_lookup_worker(bfd *);
  276.  
  277. */
  278.  
  279. FILE *
  280. DEFUN(bfd_cache_lookup_worker,(abfd),
  281.       bfd *abfd)
  282. {
  283.   if (abfd->my_archive) 
  284.       {
  285.     abfd = abfd->my_archive;
  286.       }
  287.   /* Is this file already open .. if so then quick exit */
  288.   if (abfd->iostream) 
  289.       {
  290.     if (abfd != cache_sentinel) {
  291.       /* Place onto head of lru chain */
  292.       snip (abfd);
  293.       cache_sentinel = insert(abfd, cache_sentinel);
  294.     }
  295.       }
  296.   /* This is a BFD without a stream -
  297.      so it must have been closed or never opened.
  298.      find an empty cache entry and use it.  */
  299.   else 
  300.       {
  301.  
  302.     if (open_files >= BFD_CACHE_MAX_OPEN) 
  303.         {
  304.           close_one();
  305.         }
  306.  
  307.     BFD_ASSERT(bfd_open_file (abfd) != (FILE *)NULL) ;
  308.     fseek((FILE *)(abfd->iostream), abfd->where, false);
  309.       }
  310.   bfd_last_cache = abfd;
  311.   return (FILE *)(abfd->iostream);
  312. }
  313.